/**
  * Created by Joshua Conero(O'Uymas) on 2016/11/28 0028.
  * QQ: 957376074
  * E-mail: Brximl@163.com
  */
import java.awt.Color
import java.io.File
import scala.swing._
import scala.swing.event._
import scala.swing.TabbedPane._
object CrawlerGUI {
  val br = "\r\n"
  var helper = new Common
  val sTReport = helper.dateDiff()
  val complierCount = helper.complierCount("create")
  helper.reportLog("用户正在登陆应用，或者app进行了编译(:"+sTReport.sysTimeBegin.toString+"),次数"+complierCount)

  def main(args: Array[String]): Unit = {
    val fChooser = new FileChooser(new File("."))
    fChooser.title = "文件选择"

    val tab = new TabbedPane(){
      name = "tab1"
      background = Color.gray
      // 欢迎页面
      val weclome = new FlowPanel {
        border = Swing.EmptyBorder(5,5,5,5)
        contents += new Button("test")
      }

      // 监听事件- SelectionChanged 被选事件
      listenTo(this.selection)
      reactions += {
        case event.SelectionChanged(source) =>
          if (selection.index >= 0) {
            val page = selection.page
            println(page.title,page.index)
          }
      }

      pages += new Page("欢迎使用Crawler",weclome)
      visible = true
    }
    val crawler = new home(tab,helper)
    val homeMenu = new MenuBar(){
      name = "starta"
      val file = new Menu("文件"){
        val newfile = new MenuItem("新建文件")
        val open = new MenuItem("打开文件")
        val dir = new MenuItem("打开目录")
        val setting = new MenuItem("设置")
        val quitAtion = new MenuItem("退出")
        contents += newfile
        contents += open
        contents += dir
        contents += new Separator
        contents += setting
        contents += new Separator
        contents += quitAtion

        listenTo(newfile,open,setting,quitAtion)
        reactions += {
          case ActionEvent(`newfile`) =>
            crawler.mainPage
          case ActionEvent(`open`) =>
            fChooser.showDialog(this,"确定")
          case ActionEvent(`setting`) => // 显示新的iframe框
            new Setting(helper)
          case ActionEvent(`quitAtion`) => // 退出程序
            beforeQuit()
            System.exit(0)
        }
      }
      val edit = new Menu("编辑")
      val tool = new Menu("工具"){
        val ip = new MenuItem("ip"){
          tooltip = "通过域名后去对应的IP/获取当前的IP信息"
        }
        val url = new MenuItem("url"){
          tooltip = "查询url的基本信息"
        }
        val log_btn = new MenuItem("日志")
        val phper = new MenuItem("phper"){
          tooltip = "PHP 管理工具"
        }
        contents += ip
        contents += url
        contents += new Separator
        contents += log_btn
        contents += new Separator
        contents += phper

        listenTo(ip,url,log_btn,phper)
        reactions += {
          case ActionEvent(`ip`) =>
            new tool(tab,"ip")
          case ActionEvent(`url`) =>
            new tool(tab,"url")
          case ActionEvent(`log_btn`) =>
            new tool(tab,"log_btn")
          case ActionEvent(`phper`) =>
            pageTo("PHP.home")
        }
      }
      val about = new Menu("关于"){
        val aboutApp = new MenuItem("crawler")
        contents += aboutApp
        listenTo(aboutApp)
        reactions += {
          case ActionEvent(`aboutApp`) =>
            val cmpArr = helper.getComplier()
            val msg =
              br+"作者："+helper.author+
                br+"版本号："+helper.version+
                br+"创建时间："+helper.createTime+
                br+"版权信息：Conero@2016-"+helper.date("yyyy")+
                br+"项目起源：来源于对Scala语言的学习，关注JVM新语言，网络爬虫，scala-swing，scala对xml格式的支持等"+
                br+"scala最低版本：V2.11.8; 官网API学习 http://www.scala-lang.org/api/2.11.8/"+
                br+"本次运行时间记录："+sTReport.time2Str()+"("+sTReport.diffTime("s").toString+"s/"+sTReport.diffTime().toString+"ms)。开机时间 "+ sTReport.getStartTime()+
                br+"编译记录："+cmpArr(0)+"/"+cmpArr(1)
            Dialog.showMessage(this,msg,"关于 crawler 应用介绍")
        }
      }
      contents += file  // 文件
      contents += edit  // 编辑
      contents += tool  // 工具
      contents += about // 关于
      visible = true
    }
    val home = new MainFrame {
      title = "Crawler 助手"
      background = Color.red
      preferredSize = new Dimension(800, 500)
      menuBar = homeMenu  // 主菜单
      contents = tab      // 主主tab内容
      visible = true
      listenTo(this)
      reactions += {
        case e:event.WindowClosing =>
          beforeQuit()
      }
    }
  }
  // 页面跳转
  def pageTo(cls:String = ""):Unit = {
    if(cls == "") return
    try{
      Class.forName(cls).newInstance()
    }
  }
  // 关闭后处理
  def beforeQuit(plus:String = ""):Unit = {
    var log = "用户已经退出了应用，在线时长:"+sTReport.time2Str()+"("+sTReport.diffTime("s").toString+"s/"+sTReport.diffTime().toString+"ms)。"
    if(plus != "") log = log + "\r\n"+ plus
    helper.reportLog(log)
  }
}
